package com.tgky.ybzb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.tgky.ybzb.config.GetYml;
import com.tgky.ybzb.entity.FileInfo;
import com.tgky.ybzb.entity.Rwghmx;
import com.tgky.ybzb.entity.RwghmxZbs;
import com.tgky.ybzb.enums.MxTypeEnum;
import com.tgky.ybzb.service.FileInfoService;
import com.tgky.ybzb.utils.CommonUtil;
import com.tgky.ybzb.utils.MPUtil;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.tgky.ybzb.entity.Rwghmx;
import com.tgky.ybzb.service.IRwghmxService;
//import com.tiamo.dt.dev.common.pojo.result.Result;
//import com.tiamo.dt.dev.common.pojo.param.IdParam;
//import com.tiamo.dt.dev.db.utils.MPUtil;
//import com.tiamo.dt.dev.db.pojo.page.PageParam;


import com.tgky.ybzb.common.IdParam;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.common.Result;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务规划模型表 前端控制器
 *
 * @author XYP
 * @since 2024-07-22
 */
@RestController
@AllArgsConstructor
@Slf4j
@Api(value = "任务规划模型表", tags = "任务规划模型表接口")
@RequestMapping("rwghmx")
public class RwghmxController {
    @Resource
    private IRwghmxService rwghmxService;
    @Resource
    private FileInfoService fileInfoService;
    @Resource
    private GetYml getYml;

//    @PostMapping("/uploadghMxFile")
//    @ApiOperationSupport(order = 1)
//    @ApiOperation(value = "上传规划模型文件（暂未使用）")
//    public Result uploadMxFile(MultipartFile file){
//        String path=getYml.getSavePath();
//        String savePath= CommonUtil.uploadMxFile(file,path);
//        return Result.success(savePath);
//    }
    /**
     * 调用 任务规划模型
     */
    @PostMapping("/invokeRwghmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "调用任务规划模型", notes = "传入rwghmx")
    public Result invokeRwghmx(@RequestBody Rwghmx rwghmx) {

        return Result.success();
    }


    /**
     * 评估 任务规划模型
     */
    @PostMapping("/assessRwghmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "评估任务规划模型", notes = "传入rwghmx")
    public Result assessRwghmx(@RequestBody Rwghmx rwghmx) {

        return Result.success();
    }


    /**
     * 开始训练 任务规划模型
     */
    @PostMapping("/startTrainingRwghmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "开始训练 任务规划模型", notes = "传入rwghmx")
    public Result startTraining(@RequestBody Rwghmx rwghmx) {

        return Result.success();
    }


    /**
     * 结束训练 任务规划模型
     */
    @PostMapping("/endTrainingRwghmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "结束训练 任务规划模型", notes = "传入rwghmx")
    public Result endTraining(@RequestBody Rwghmx rwghmx) {

        return Result.success();
    }


    /**
     * 训练模型保存 任务规划模型
     */
    @PostMapping("/saveTrainRwghmx")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "训练模型保存 任务规划模型", notes = "传入rwghmx")
    public Result saveTrainRwghmx(@RequestBody Rwghmx rwghmx) {

        return Result.success();
    }


    /**
     * 上传 任务规划模型
     *
     * @param file
     * @return
     */
    @PostMapping("/importRwghmxModel")
    @ApiOperation(value = "上传任务规划模型")
    public Result importModel(MultipartFile file) {


        // TODO 返回上传地址
        return Result.success();
    }


    /**
     * 配置指标树 任务规划模型
     *
     * @return
     */
    @PostMapping("/configIndicatorTree ")
    @ApiOperation(value = "配置指标树-任务规划模型")
    public Result configIndicatorTree(@RequestBody RwghmxZbs rwghmxZbs) {


        return Result.success();
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入rwghmx")
    public Result save(@RequestBody Rwghmx rwghmx) {
        if (rwghmx == null) {
            return Result.error("参数异常");
        }
        return Result.status(rwghmxService.saveRwghmxAndFile(rwghmx));
    }

    /**
     * 修改 任务规划模型表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入rwghmx")
    public Result update(@RequestBody Rwghmx rwghmx) {
        if (rwghmx == null) {
            return Result.error("参数异常");
        }
        return Result.status(rwghmxService.updateById(rwghmx));
    }

    /**
     * 删除 任务规划模型表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除", notes = "传入ids")
    public Result remove(@RequestBody IdParam<Long> idParam) {
        if (idParam == null) {
            return Result.error("参数异常！");
        }
        for (Long id:idParam.getIds()){
            Map<String,Object> map=new HashMap<>();
            map.put("type", MxTypeEnum.RWGHMX.toString());
            map.put("table_id",id);
            fileInfoService.removeByMap(map);
        }
        return Result.status(rwghmxService.removeByIds(idParam.getIds()));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情", notes = "传入id")
    public Result<Rwghmx> detail(Long id) {
        if (id == null) {
            return Result.error("参数异常");
        }
        Rwghmx detail = rwghmxService.getById(id);
        return Result.successData(detail);
    }

    /**
     * 查询 任务规划模型表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询", notes = "传入rwghmx")
    public Result list(PageParam pageParam, Rwghmx rwghmx) {
        List<Rwghmx> list = rwghmxService.list(rwghmxService.getQueryWrapper(pageParam, rwghmx));
        return Result.successData(list);
    }

    /**
     * 分页 任务规划模型表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页", notes = "传入rwghmx")
    public Result<IPage> page(PageParam pageParam, Rwghmx rwghmx) {
        IPage<Rwghmx> pages = rwghmxService.page(MPUtil.getPage(pageParam), rwghmxService.getQueryWrapper(null, rwghmx));
        pages.getRecords().stream().forEach(item->{
            QueryWrapper<FileInfo> fileInfoQueryWrapper=new QueryWrapper<>();
            fileInfoQueryWrapper.lambda().eq(FileInfo::getTableId,item.getMxid());
            fileInfoQueryWrapper.lambda().eq(FileInfo::getType, MxTypeEnum.RWGHMX.toString());
            FileInfo fileInfo=fileInfoService.getOne(fileInfoQueryWrapper);
            item.setFileInfo(fileInfo);
        });
        return Result.successData(pages);
    }

}
